package com.lc.controller;

import com.lc.constant.Constants;
import com.lc.pojo.dto.UploadFileDto;
import com.lc.result.Result;
import com.lc.result.ResultUtils;
import com.lc.utils.AliOSSUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Description: 文件上传
 * @Author lc
 */

@RestController
@RequestMapping("/pms/files")
public class PmsFilesController {

    @Resource
    private AliOSSUtil aliOSSUtil;

    //文件上传接口
    @PostMapping("/uploadFile")
    public Result uploadFile(MultipartFile file,Integer type,HttpServletRequest request) throws IOException {
        String token = request.getHeader(Constants.Redis.PREFIX_TOKEN);
        if(!StringUtils.hasLength(token)){
            return ResultUtils.returnFail("文件上传前，请登录");
        }

        UploadFileDto fileDto = new UploadFileDto();

        //把图片直接上传到阿里oss服务中
        String url = aliOSSUtil.uploadFile(file);
        fileDto.setFilePath(url);

        return ResultUtils.returnDataSuccess(fileDto);
    }

}
